﻿Shader "Unlit/01 surface angle"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _MainColor ("Main Color", Color) = (1,1,1,1) 
        _OutlineWidth ("Outline Width", float) = 1
        _OutlineColor ("Outline Color", Color) = (0,0,0,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 normalWS : TEXCOORD1;
                float3 viewDirWS : TEXCOORD2;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _MainColor;
            float _OutlineWidth;
            fixed4 _OutlineColor;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.normalWS = mul(v.normal, (float3x3)unity_WorldToObject);
                float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                o.viewDirWS = _WorldSpaceCameraPos.xyz - worldPos; 
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed3 normalWS = normalize(i.normalWS);
                fixed3 viewDirWS = normalize(i.viewDirWS);

                fixed3 col = tex2D(_MainTex, i.uv).rgb*_MainColor.rgb;
                float outline = 1 - max(0,dot(viewDirWS,normalWS));
                fixed3 outlineCol = pow(outline, _OutlineWidth)*_OutlineColor.rgb;
                return fixed4(col+outlineCol,1);
            }
            ENDCG
        }
    }
}
